<template>
  <div class="settings-container">
    <div class="app-container">
      <el-card>
        <template>
          <div class="dashboard-container">
            <div class="app-container">
              <el-card>
                <el-tabs>
                  <!-- 放置页签 -->
                  <el-tab-pane label="角色管理">
                    <!-- 新增角色按钮 -->
                    <el-row style="height:60px">
                      <el-button
                        icon="el-icon-plus"
                        size="small"
                        type="primary"
                        @click="hAdd"
                      >新增角色</el-button>
                    </el-row>
                    <!-- 表格 -->
                    <el-table :data="rloes" border>
                      <el-table-column label="序号" width="50" type="index" />
                      <el-table-column label="角色名称" width="240" prop="name" />
                      <el-table-column label="描述" prop="description" />
                      <el-table-column label="操作">
                        <template #default="{row}">
                          <el-button size="small" type="success">分配权限</el-button>
                          <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                          <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                        </template>
                      </el-table-column>
                    </el-table>

                    <el-row type="flex" justify="center" align="middle" style="height: 60px">
                      <!-- 分页组件 -->
                      <el-pagination
                        :current-page="pageParams.page"
                        :page-sizes="[2,3,5,10,20]"
                        :page-size="pageParams.pagesize"
                        layout="total,sizes, prev, pager, next"
                        :total="total"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                      />
                    </el-row>
                  </el-tab-pane>
                </el-tabs>
              </el-card>
            </div>
          </div>
        </template>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑':'添加'"
        :visible.sync="showDialog"
        @closed="resetForm"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRloes, addRloes, updateRloes, delRloes } from '@/api/settings'
export default {
  name: 'Settings',

  data() {
    return {
      pageParams: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      rloes: [],
      showDialog: false,
      isEdit: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delRloes(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          if (this.list.length === 1) {
            this.pageParams.page--
            if (this.pageParams.page <= 0) {
              this.pageParams.page = 1
            }
          }
          this.loadRoles()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    resetForm() {
      // 重置效验规则
      // resetFields() 只是让表单里的数据回到初始化，
      // 初始化有数据，就回到有数据的时候
      // 表单一开始没数据，就清空
      this.$refs.roleForm.resetFields()
      // 手动重置数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    hSubmit() {
    //  兜底效验
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        // 效验成功发送请求
        this.isEdit ? this.doEidt() : this.doAdd()
      })
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    hEdit(row) {
      this.showDialog = true
      this.isEdit = true
      // 数据回显   直接赋值会导致修改编辑弹层时外部页面也会修改，指向同一个地址，
      // 所以使用浅拷贝，放在对象里，展开运算符
      this.roleForm = { ...row }
    },
    async doEidt() {
      const res = await updateRloes(this.roleForm)
      console.log(res, '添加成功')
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 重新渲染页面
      this.loadRoles()
      // 关闭弹层
      this.showDialog = false
    },
    async doAdd() {
      const res = await addRloes(this.roleForm)
      console.log(res, '添加成功')
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 因为代码从上往下执行
      // 添加时，用的是旧的总条数去运算，不能直接跳转到最大页码值，所有total++，用新的总条数去运算
      this.total++
      // 求最大页码值    总条数除每页的条数
      const Maxpage = Math.ceil(this.total / this.pageParams.pagesize)
      // 改变当前的页码值
      this.pageParams.page = Maxpage
      // 重新渲染页面
      this.loadRoles()
      // 关闭弹层
      this.showDialog = false
    },
    async loadRoles() {
      try {
        const res = await getRloes(this.pageParams)
        console.log(res, '获取角色列表')
        this.rloes = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      this.pageParams.pagesize = val
      this.pageParams.page = 1
      this.loadRoles()
    },
    handleCurrentChange(val) {
      this.pageParams.page = val
      this.loadRoles()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
